package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.StaffDao;
import com.aaa.qy127.entity.Staff;
import com.aaa.qy127.util.JDBCUtil;
import com.aaa.qy127.util.UploadUtil;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("StaffDaoImpl")
public class StaffDaoImpl implements StaffDao {
    /**
     * 根据 staffId 和password 查询 Staff信息
     * @return
     */
    @Override
    public Staff getOneStaffByStaffIdAndPassword(String staffId,String password) {

        //System.out.println("StaffDaoImpl----"+staffId+"-----"+password);

        String sql  = "select id,staffId,staffName,password,phone,idCard,address,createdTime,resignedTime,status,roleId,deptid,url from t_staff where staffId = ? and password = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, staffId, password);

        //System.out.println("StaffDaoImpl----"+maps);

        if (maps.size() >0){
            Map<String, Object> map = maps.get(0);
            Staff staff = new Staff((Integer)map.get("id"),(Integer)map.get("staffId"),(String)map.get("staffName"),(String)map.get("password"),(String)map.get("phone"),(String)map.get("idCard"),(String)map.get("address"),(Date) map.get("createdTime"),(Date)map.get("resignedTime"),(Integer) map.get("status"),(Integer)map.get("roleId"),(Integer)map.get("deptid"),(String)map.get("url"));

            //System.out.println("StaffDaoImpl----"+staff);

            return staff;
        }
        return null;
    }

    /**
     * 查询所有的员工信息
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllStaff(Staff staff) {

        StringBuffer sql = new StringBuffer("select ts.id,ts.staffId,ts.staffName,ts.password,ts.phone,ts.idCard,ts.address,ts.createdTime,ts.resignedTime,ts.`status`,ts.roleId,tr.rname,ts.deptid,td.dname,ts.url from t_staff ts INNER JOIN t_role tr ON ts.roleId = tr.rid  INNER JOIN t_dept td ON ts.deptid = td.deptid where 1=1");
        if (staff.getStaffId() != null && staff.getStaffId() != 0){
            sql.append(" and ts.staffId like '%").append(staff.getStaffId()+"%'");
        }

        if (staff.getStaffName() != null){
            sql.append(" and ts.staffName like '%").append(staff.getStaffName()+"%'");
        }
        if (staff.getDeptid() != null && staff.getDeptid() != 0){
            sql.append(" and ts.deptid like '%").append(staff.getDeptid()+"%'");
        }
        sql.append(" limit ?,?");
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql.toString(), (staff.getPage()-1)*staff.getLimit(),staff.getLimit());
        return maps;
    }

    /**
     * 查询所有信息的数量
     * @return
     */
    @Override
    public int getAllCount(Staff staff) {
        StringBuffer sql = new StringBuffer("select COUNT(*) from t_staff ts where 1=1");
        if (staff.getStaffId() != null && staff.getStaffId() != 0){
            sql.append(" and ts.staffId like '%").append(staff.getStaffId()+"%'");
        }

        if (staff.getStaffName() != null){
            sql.append(" and ts.staffName like '%").append(staff.getStaffName()+"%'");
        }
        if (staff.getDeptid() != null && staff.getDeptid() != 0){
            sql.append(" and ts.deptid like '%").append(staff.getDeptid()+"%'");
        }

        List<List> lists = JDBCUtil.queryList(sql.toString());
        Object o = lists.get(0).get(0);
        int i = Integer.parseInt(o + "");
        return i;
    }

    /**
     * 查询出所有staffid中最大值
     * @return
     */
    @Override
    public int getMax() {
        String sql = "select MAX(staffId) from t_staff";
        List<List> lists = JDBCUtil.queryList(sql);
        if (lists.size() > 0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    /**
     * 修改状态  在职 / 离职
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateOneStatus(Integer id, Integer status) {
        //当status 的值 是1（在职） 的时候 变成0（离职）
        //当status 的值 是0（离职） 的时候 变成1（在职）
        if (status == 1){
            status = 0;
        }else {
            status = 1;
        }
        String sql = "update t_staff set status =? where id= ? ";
        int i = JDBCUtil.update(sql, status, id);
        return i;
    }

    /**
     * 新增一条信息
     * @param staff
     * @return
     */
    @Override
    public int insertOneStaff(Staff staff) {
        String sql = "insert into t_staff values(null,?,?,?,?,?,?,?,?,?,?,?,?)";
        //获取一个当前的国际化日期格式
        Date date = new Date();
        int i = JDBCUtil.update(sql, staff.getStaffId(), staff.getStaffName(), staff.getPassword(), staff.getPhone(), staff.getIdCard(), staff.getAddress(), date, null, 1, staff.getRoleId(), staff.getDeptid(), staff.getUrl());
        return i;
    }

    /**
     * 修改一条信息
     * @param staff
     * @return
     */
    @Override
    public int updateOneStaff(Staff staff) {
        String sql ="update t_staff set staffName=?,password=?,phone=?,idCard=?,address=?,roleId=?,deptid=? where id=?";
        int i = JDBCUtil.update(sql,staff.getStaffName(),staff.getPassword(),staff.getPhone(),staff.getIdCard(),staff.getAddress(),staff.getRoleId(),staff.getDeptid(),staff.getId());
        return i;
    }

    /**
     * 根据 根据rid查询 staff信息
     * @return
     */
    @Override
    public List<Map<String, Object>> getStaffByRid(String rid) {
        String sql = "select id,staffId,staffName,`password`,phone,idCard,address,createdTime,resignedTime,`status`,roleId,deptid,url from t_staff where roleId = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, rid);
        if (maps.size() >0){
            return maps;
        }
        return null;
    }

    /**
     * 根据穿过俩的身份证号 查询staff信息
     * @param staff
     * @return
     */
    @Override
    public List<Map<String, Object>> getStaffByIdCard(Staff staff) {
        String sql = "select idCard from t_staff where idCard = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, staff.getIdCard());
        if (maps.size() >0){
            return maps;
        }
        return null;
    }

    /**
     * 查询staff 表中管理元的个数
     * @return
     */
    @Override
    public int getCountGroupBYRoleId() {
        String sql ="select COUNT(*) from t_staff where roleId = 1 GROUP BY roleId";
        List<List> lists = JDBCUtil.queryList(sql);
        if (lists.size()>0){
            Object o = lists.get(0).get(0);
            //System.out.println(o);
            int i = Integer.parseInt(o+"");
            //System.out.println(i);
            return i;
        }
        return 0;
    }

    /**
     * 根据staffid 和 idCard 查询 员工信息
     * @param staff
     * @return
     */
    @Override
    public List<Map<String, Object>> getOneStaffByIdAndIdCard(Staff staff) {
        String sql = "select id,staffId,staffName,`password`,phone,idCard,address,createdTime,resignedTime,`status`,roleId,deptid,url from t_staff where id = ? AND idCard = ?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql, staff.getId(), staff.getIdCard());
        if (maps.size() >0){
            return maps;
        }
        return null;
    }

    @Override
    public int updatePassword(Integer staffId, String password) {
        String sql="update t_staff set password=? where staffId=?";
        int i = JDBCUtil.update(sql,password,staffId);
        return i;
    }

    /**
     * 获取当前登陆人的信息
     * @param request
     * @return
     */
    @Override
    public List<Map<String, Object>> getNowLoginStaffInformation(HttpServletRequest request) {
        Staff staff1 = (Staff)request.getSession().getAttribute("staff");
        Integer staffId = staff1.getStaffId();
        System.out.println("获取当前登录人的信息："+staff1);
        String sql = "SELECT ts.staffId,ts.staffName,td.dname,ts.createdTime,tr.rname,ts.phone,ts.idCard,ts.address from t_staff ts,t_dept td,t_role tr where ts.deptid=td.deptid and ts.roleId=tr.rid and ts.staffId=?";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql,staffId);
        return maps;
    }

    /**
     * 更新当前登录人的手机号和地址
     * @param request
     * @return
     */
    @Override
    public int updateStaffPhoneAndAddress(Staff staff,HttpServletRequest request) {
        Staff staff1 = (Staff)request.getSession().getAttribute("staff");

        Integer staffId = staff1.getStaffId();
        String sql = "update t_staff set phone=?,address=? where staffId=?";
        int i = JDBCUtil.update(sql, staff.getPhone(), staff.getAddress(), staffId);
        return i;
    }

    @Override
    public Map imgUpload(HttpServletRequest request, MultipartFile file) {
        //获取登录人id
        Staff staff1 = (Staff)request.getSession().getAttribute("staff");
        Integer staffId = staff1.getStaffId();
        //获取上传头像的url
        String url = UploadUtil.upload(file);
        String sql = "update t_staff set url=? where staffId=?";
        int i = JDBCUtil.update(sql, url,staffId);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("url",url);
        return map;
    }
}
